package com.qsl.ssyx.order.receiver;

import com.qsl.ssyx.constant.MqConst;
import com.qsl.ssyx.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;


/**
 * mq消费者-
 */
@Component // 配置类
public class OrderReceiver {

    @Autowired
    private OrderInfoService orderInfoService;

    //  支付成功 rabbitMq发送消息①修改订单状态（待发货），②减库存，③ 删除购物车已购买的商品记录   ==>   交换机,路由key,消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER_PAY, declare = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER_DIRECT),
            key = {MqConst.ROUTING_CANCEL_ORDER})
    )
    public void orderPay(String orderNo, Message message, Channel channel) throws IOException { // 消息，信道
        if (!StringUtils.isEmpty(orderNo)) {
            orderInfoService.orderPay(orderNo);
        }
        // ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
